package com.henushang.ranking.controller.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.henushang.ranking.controller.BaseController;
import com.henushang.ranking.domain.CategoryInfo;
import com.henushang.ranking.domain.RankingData;
import com.henushang.ranking.domain.RankingDataResponseVo;
import com.henushang.ranking.domain.WebsiteInfo;
import com.henushang.ranking.service.CategoryService;
import com.henushang.ranking.service.RankingDataService;
import com.henushang.ranking.service.WebsiteService;

@Controller
@RequestMapping("ranking")
public class RankingDataController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RankingDataService rankingDataService;

    @Autowired
    private WebsiteService websiteService;

    @RequestMapping(value = { "/{id}" }, method = RequestMethod.GET)
    public String rankingDataDetail(@PathVariable String id, Model model) {
        RankingData rankingData = rankingDataService.getRankingDataById(id);
        CategoryInfo categoryInfo = categoryService.getById(rankingData.getCategory_id());
        WebsiteInfo websiteInfo = websiteService.getById(rankingData.getSite_id());

        Map<WebsiteInfo, List<RankingData>> siteRankingData = rankingDataService
                .getRankingDataByCategory(rankingData.getCategory_id());

        RankingDataResponseVo responseVo = new RankingDataResponseVo(rankingData, categoryInfo,
                websiteInfo);

        model.addAttribute("ranking_response_vo", responseVo);
        model.addAttribute("ranking_map", siteRankingData);

        addNavData(model, categoryService);
        return "/news_detail";
    }
}
